package treeSet;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSet3 {
	public static void main(String[] args) {
		Set<Creature> set=new TreeSet<Creature>(new MyComparetor());
//		(new Comparator<Creature>(){
//
//			@Override
//			public int compare(Creature o1, Creature o2) {
//				if(o1.age==o2.age)
//				    return o1.name.compareTo(o2.name);
//				else 
//					return o1.age-o2.age;
//			}});
		set.add(new Creature("Cat",6));
		set.add(new Creature("butterfly",7));
		set.add(new Creature("ant",6));
		set.add(new Creature("ox",8));
		set.add(new Creature("Cat",6));
		set.add(new Creature("fly",10));
		
		for (Creature creature : set) {
			
			System.out.println(creature);
		}
	}
    
}
class MyComparetor implements Comparator<Creature>{

	@Override
	public int compare(Creature o1, Creature o2) {
		if(o1.age==o2.age)
		    return o1.name.compareTo(o2.name);
		else 
			return o1.age-o2.age;
	}
       
}
class Creature{
	String name;
	int    age;
	public Creature(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.name+"\t"+this.age;
	}
}